﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThuVien;
using System.Web.UI;
using System.Web.UI.WebControls;

public class BasePage : System.Web.UI.Page
{
    public static Logger log;

    public User UserInfo
    {
        get
        {
            if (HttpContext.Current.Session["UserInfo"] == null)
            {
                User objUser = new User();
                HttpContext.Current.Session["UserInfo"] = (object)objUser;
            }
            return (User)HttpContext.Current.Session["UserInfo"];
        }
        set
        {
            HttpContext.Current.Session["UserInfo"] = value;
        }
    }

    public void GetListPageSize(DropDownList ddl)
    {
        string[] arrPageSize = FunctionsUtils.GetConfigValue("ListPageSizes").Split(',');
        for (int i = 0; i < arrPageSize.Length; i++)
        {
            ddl.Items.Add(new ListItem(arrPageSize[i], arrPageSize[i]));
        }
        FunctionsUtils.SelectItemInControlList(ddl, FunctionsUtils.GetConfigValue("PageSize"), SelectBy.Value);
    }

    public void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
    where T : Control
    {
        foreach (Control control in controlCollection)
        {
            if (control is T) // This is cleaner
                resultCollection.Add((T)control);

            if (control.HasControls())
                GetControlList(control.Controls, resultCollection);
        }
    }

    public void RemoveSortCss()
    {
        List<LinkButton> allControls = new List<LinkButton>();
        GetControlList<LinkButton>(Page.Controls, allControls);
        foreach (var childControl in allControls)
        {
            if (((LinkButton)childControl).CommandArgument.Length > 0)
                ((LinkButton)childControl).CssClass = "GridHeader";
        }
    }

    public void SetSortAtributeForLinkButton(string sortBy, string sortDirect)
    {
        string css = "sortDesc";
        if (sortDirect.Equals("ASC") == true)
        {
            css = "sortAsc";
        }

        List<LinkButton> allControls = new List<LinkButton>();
        GetControlList<LinkButton>(Page.Controls, allControls);
        foreach (var childControl in allControls)
        {
            if (((LinkButton)childControl).CommandName.Equals(sortBy) == true)
            {
                //((LinkButton)childControl).CommandName = sortBy;
                ((LinkButton)childControl).CommandArgument = sortDirect;
                ((LinkButton)childControl).CssClass = "GridHeader " + css;
            }
        }
    }

    public string GetImageUrl(object productISN, object imageFileName)
    {
        return string.Format("{0}{1}/{2}", FunctionsUtils.GetConfigValue("ProductImageUrl"), productISN, imageFileName);
    }
}
